package com.buct.coder.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buct.coder.facade.request.PageRequest;
import com.buct.coder.facade.response.ApiResult;
import com.buct.coder.model.TrainInfo;
import com.buct.coder.service.ITrainInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xinzi
 * @since 2022-08-17
 */
@RestController
@RequestMapping("/trainInfo")
public class TrainInfoController {

    @Autowired
    private ITrainInfoService trainInfoService;

    @PostMapping("/list")
    public ApiResult<Page<TrainInfo>> findPages(@RequestBody PageRequest pageRequest) {
        return ApiResult.success(trainInfoService.page(
                new Page<>(pageRequest.getPage(), pageRequest.getSize())
        ));
    }

    @DeleteMapping("/info/{trainId}")
    public ApiResult<Boolean> deleteTrain(@PathVariable("trainId") Long trainId) {
        return ApiResult.success(trainInfoService.removeById(trainId));
    }

    @PutMapping("/info")
    public ApiResult<Boolean> addTrain(@RequestBody TrainInfo trainInfo) {
        return ApiResult.success(trainInfoService.save(trainInfo));
    }

    @PatchMapping("/info")
    public ApiResult<Boolean> updateTrain(@RequestBody TrainInfo trainInfo) {
        return ApiResult.success(trainInfoService.updateById(trainInfo));
    }

}
